#include <stdio.h>
#include <stdlib.h>
#include "linktest.h"


link_list *creatnode()
{
	link_list *H=(link_list *)malloc(sizeof(link_list));
	H->data=-1;
	H->next=NULL;

	return H;
}

int headinsertnode(link_list *H)
{
	link_list *p=(link_list *)malloc(sizeof(link_list));
	int inputdata;
	scanf("%d",&inputdata);
	p->data=inputdata;
	p->next=H->next;
	H->next=p;

	return 0;
}

int tailinsertnode(link_list *H)
{
	link_list *p=(link_list *)malloc(sizeof(link_list));
	int inputdata;
	scanf("%d",&inputdata);
	p->data=inputdata;
	p->next=NULL;
	while(H->next!=NULL)
	{
		H=H->next;
	}
	H->next=p;

	return 0;
}

int posinsert(link_list *H)
{
	int posinput;
	scanf("%d",&posinput);
	for(int i=0;i<posinput-1;i++)
	{
		H=H->next;
	}
	link_list *p=(link_list *)malloc(sizeof(link_list));
	int inputdata;
	scanf("%d",&inputdata);
	p->data=inputdata;
	p->next=H->next;
	H->next=p;

	return 0;
}

int headdeletenode(link_list *H)
{
	link_list *p=H->next;
	H->next=p->next;

	free(p);

	return 0;
}

int taildeletenode(link_list *H)
{
	while(H->next->next!=NULL)
	{
		H=H->next;
	}
	link_list *p=H->next;
	H->next=NULL;

	free(p);

	return 0;
}

int posdelete(link_list *H)
{
	int posinput;
	scanf("%d",&posinput);
	for(int i=0;i<posinput-1;i++)
	{
		H=H->next;
	}
	link_list *p=H->next;
	H->next=p->next;

	free(p);

	return 0;
}

int modnode(link_list *H,int pos)
{
	for(int i=0;i<pos;i++)
	{
		H=H->next;
	}
	int inputdata;
	scanf("%d",&inputdata);
	H->data=inputdata;

	return 0;
}

int posview(link_list *H,int pos)
{
	for(int i=0;i<pos;i++)
	{
		H=H->next;
	}

	printf("the %d pos is : %d \n",pos,H->data);
	
	return 0;
}

int view(link_list *H)
{
	if(H->next==NULL)
	{
		printf("link is null !!\n");
		return -1;
	}
	while(H->next!=NULL)
	{
		H=H->next;
		printf("%d ",H->data);
	}
	printf("\n");
	return 0;

}
